﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    class Camera2D
    {
        protected float _rotation;
        protected float _zoom; // Zoom in by increasing this variable, zoom out by decreasing (1.0 is no zoom)
        protected Vector2 _position;
        protected Matrix _transform;

        public Camera2D()
        {

        }

        public void Initialize(Vector2 position)
        {
            _zoom = 1.0f;
            _rotation = 0.0f;
            _position = position;
        }

        public virtual void Update(GameTime gameTime)
        {

        }

        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; }
        }

        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = Common.NormalizeAngle(value); }
        }

        /// <summary>Move the camera relative to itself</summary>
        public void MoveRelative(Vector2 amount)
        {
            _position += amount;
        }

        /// <summary>Move the camera relative to the world</summary>
        public void MoveWorld(Vector2 amount)
        {
            var moveLength = amount.Length();
            var moveAngleWorld = Common.GetVectorAngle(amount);
            var moveAngleRelative = Common.NormalizeAngle(moveAngleWorld - this.Rotation);
            MoveRelative(Common.VectorFromAngle(moveAngleRelative, moveLength));
        }

        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public Matrix get_transformation(GraphicsDevice graphicsDevice)
        {
            _transform =
              Matrix.CreateTranslation(new Vector3(-_position.X, -_position.Y, 0)) *
                                         Matrix.CreateRotationZ(_rotation) *
                                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
                                         Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f, graphicsDevice.Viewport.Height * 0.5f, 0));
            return _transform;
        }

    }
}
